<template>
    <div class="page_1">
        <div class="top_box">
            <el-button type="warning" icon="el-icon-plus" size="small" @click="add">添加</el-button>
        </div>
        <el-table :data="data" style="width: 100%" border>
            <el-table-column prop="name" label="题目标题">
            </el-table-column>
            <!-- <el-table-column label="题目大类">
                <template slot-scope="{row}">
                    {{ comCateType(row.cate_type) }}
                </template>
            </el-table-column> -->
            <el-table-column prop="sort" label="排序" width="100">
            </el-table-column>
            <el-table-column prop="cate_name" label="是否有图标" width="100">
                <template slot-scope="{row}">
                    {{ row.type == 1 ? "是" : "否" }}
                </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="300">
                <template slot-scope="{ row }">
                    <el-button type="warning" @click="edit(row)">编辑</el-button>
                    <el-button type="danger" @click="del(row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 表单 -->
        <el-dialog :visible.sync="dialogVisible" :title="formTitle" append-to-body width="900px" @close="formReset">
            <el-form ref="form" :rules="rules" :model="form" label-width="120px">
                <el-row type="flex" :gutter="10">
                    <!-- <el-col :span="12">
                        <el-form-item label="题目大类" prop="cate_type">
                            <el-select v-model="form.cate_type" placeholder="请选择题目类型">
                                <el-option v-for="item in timuType" :key="item.id" :label="item.name" :value="item.id" />
                            </el-select>
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="12">
                        <el-form-item label="题目分类名称" prop="name">
                            <el-input v-model="form.name"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="是否有图标" prop="type">
                            <el-radio v-model="form.type" :label="1">是</el-radio>
                            <el-radio v-model="form.type" :label="2">否</el-radio>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="排序" prop="sort">
                            <el-input v-model.number="form.sort" placeholder="数值越大越靠前，默认数值100"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex" justify="center">
                    <el-button type="primary" @click="initiate()">添加</el-button>
                    <el-button @click="formReset()" type="primary">重置</el-button>
                </el-row>
            </el-form>
        </el-dialog>
        <el-pagination class="main-pagination" :total="total" :current-page.sync="page" :page-size="limit" layout="prev, pager, next, jumper,total" @current-change="getTopicClassification" hide-on-single-page></el-pagination>
    </div>
</template>

<script>
import {
    get, del, post
} from "@/assets/js/http.js";
export default {
    data() {
        return {
            total: 0,
            limit: 20,
            page: 1,
            data: [],
            dialogVisible: false,
            timuType: [
                {
                    id: 1,
                    name: "公路基础设施满意度评价"
                },
                {
                    id: 2,
                    name: "收费服务满意度评价"
                },
                {
                    id: 3,
                    name: "救援服务满意度评价"
                },
                {
                    id: 4,
                    name: "出行信息服务评价"
                },
                {
                    id: 5,
                    name: "运营品牌满意度评价"
                },
                {
                    id: 6,
                    name: "其它"
                },
            ],
            form: {
                sort: "",
                name: "",
                type: ""
            },
            rules: {
                sort: [
                    { required: true, message: '请输入排序数值', trigger: 'blur' },
                ],
                name: [
                    { required: true, message: '请填写题目分类名称', trigger: 'blur' }
                ],
                type: [
                    { required: true, message: '请选择是否有图标', trigger: 'change' }
                ]
            },
            formTitle: "添加"
        }
    },
    methods: {
        // 请求题目分类列表
        getTopicClassification(index = 1) {
            post({
                url: "/admin/answer.Subject/cate_index",
                data: {
                    page: index,
                    limit: this.limit,
                },
                loading: true
            }).then(res => {
                console.log(res);
                this.total = res.total


                this.data = res.data
            })
        },
        // 添加
        add() {
            this.formReset()
            this.dialogVisible = true
        },
        // 表单添加
        initiate() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    post({
                        url: "/admin/answer.Subject/edit_cate",
                        data: this.form,
                        loading: true,
                        successTip: true,
                    }).then(res => {
                    }).finally(() => {
                        this.dialogVisible = false
                        this.getTopicClassification(this.page)
                    })
                }
            });

        },
        // 修改
        edit(row) {
            this.formTitle = "修改"
            this.form = row
            this.dialogVisible = true
        },
        // 删除
        del(id) {
            this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                post({
                    url: "/admin/answer.Subject/del_cate",
                    data: {
                        id
                    },
                    loading: true,
                    successTip: true,
                }).finally(() => {
                    this.getTopicClassification()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        // 表单关闭回调
        formReset() {
            this.form = Object.assign({}, this.$options.data().form)
        },
    },
    computed: {
        comCateType() {
            return (item) => {
                let a = this.timuType.find(res => {
                    return res.id == item
                })
                return a.name
            }
        }
    },
    mounted() {
        this.getTopicClassification()
    }
}
</script>

<style></style>